﻿using System.Collections.Generic;
using UnityEngine;

namespace HEFramework
{
    public static class SummonSystem
    {
        [EntitySystem]
        public class SummonAwakeSystem : AwakeSystem<Summon>
        {
            protected override void Awake(Summon _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                var hDatas = dc.LoadDataTable<DRHero>().GetAllData();
                for (int i = 0; i < hDatas.Length; i++)
                {
                    _self.Heros.Add(hDatas[i].ID);
                }
            }
        }

        public static bool Summon(this Summon _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return false;
            }


            if (_self.Heros.Count <= 0)
            {
                return false;
            }

            var player = _self.GetSceneChild<Player>();
            if (player == null)
            {
                return false;
            }

            //判断招募金钱是否足够
            var gTable = dc.LoadDataTable<DRGlobalSettings>();
            if (player.G < gTable.GetData((int)eGlobalSetting.Summon_Hero_G).Value1)
            {
                return false;
            }

            //验证队伍数据
            var team = _self.GetSceneChild<Team>();
            if (team == null)
            {
                return false;
            }

            //清空
            _self.Lotteries.Clear();
            int max = 3;
            if (_self.Heros.Count > max)
            {
                for (int i = 0; i < max; i++)
                {
                    //重复知道抽到不同
                    while (true)
                    {
                        var id = _self.Heros[Random.Range(0, _self.Heros.Count)];
                        if (!_self.Lotteries.Contains(id))
                        {
                            _self.Lotteries.Add(id);
                            break;
                        }
                    }
                }
            }
            else
            {
                _self.Lotteries.AddRange(_self.Heros);
                int count = max - _self.Lotteries.Count;
                if (count != 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        _self.Lotteries.Add(-1);
                    }
                }
            }

            player.G -= gTable.GetData((int)eGlobalSetting.Summon_Hero_G).Value1;
            return true;
        }

        public static int Select(this Summon _self, int _id)
        {
            var team = _self.GetSceneChild<Team>();

            if (team == null)
            {
                return (int)eSummonErrorCode.System;
            }

            if (!_self.Lotteries.Contains(_id))
            {
                return (int)eSummonErrorCode.System;
            }

            var hero = team.GetHero(_id);
            if (hero != null)
            {
                var ac = hero.GetChild<AttributeComponent>();
                if (ac == null)
                {
                    return (int)eSummonErrorCode.System;
                }

                //升星
                ac.Change_RANK(1);
                _self.Lotteries.Clear();
                return (int)eSummonErrorCode.None;
            }
            else
            {
                if (team.IsTeamMax())
                {
                    return (int)eSummonErrorCode.OutLimit;
                }

                _self.Lotteries.Clear();
                team.AddHeroRandomPos(_id);
                return (int)eSummonErrorCode.None;
            }
        }
        
        public static bool IsHaveCache(this Summon _self)
        {
            return _self.Lotteries.Count > 0;
        }
    }
}